<?php
require_once APP_PATH . '/post/model.php';
function post_create_action() {
	if ($_SERVER['REQUEST_METHOD'] == 'POST') {
		//提交保存
		post_save_action();
	} else {
		//显示编辑页面
		post_show_action();
	}

}
/**
 * 显示post页面
 * @param  string
 * @return [type]
 */
function post_show_action() {
	checkLogin();
	$hash = isset($_REQUEST['hash']) ? trim($_REQUEST['hash']) : '';

	if (empty($hash)) {
		include APP_ADMIN_VIEW . 'post/create.php';
	} else {
		$hash = filter_data($hash);
		//获取数据
		$post_data = post_model_get_by_hash($hash);
		//输出
		if ($post_data) {
			include APP_ADMIN_VIEW . 'post/create.php';
		} else {
			show_not_found();
		}
	}

}

function post_save_action() {
	checkLogin();
	$hash = isset($_REQUEST['hash']) ? trim($_REQUEST['hash']) : '';
	if (empty($hash)) {
		//新建
		$title = isset($_POST['title']) ? trim($_POST['title']) : '';
		$content = isset($_POST['content']) ? trim($_POST['content']) : '';
		$category_id = isset($_POST['category_id']) ? intval($_POST['category_id']) : 0;

		$keywords = isset($_POST['keywords']) ? trim($_POST['keywords']) : '';
		$description = isset($_POST['description']) ? trim($_POST['description']) : '';
		$status = isset($_POST['status']) ? intval(trim($_POST['status'])) : 0;

		$draft = isset($_POST['draft']) ? intval(trim($_POST['draft'])) : 0;
		//获取数据
		$ret = post_model_insert($title, $category_id, $content, $status, $draft, $keywords, $description);
		if ($ret) {
			header("Location: /" . APP_ADMIN_URI . "/post/list");
		}
	} else {
		//更新
		//新建
		$title = isset($_POST['title']) ? trim($_POST['title']) : '';
		$content = isset($_POST['content']) ? trim($_POST['content']) : '';
		$category_id = isset($_POST['category_id']) ? intval($_POST['category_id']) : 0;

		$status = isset($_POST['status']) ? intval($_POST['status']) : 0;

		$draft = isset($_POST['draft']) ? intval($_POST['draft']) : 0;
		//获取数据
		$ret = post_model_update_by_hash($hash, $title, $category_id, $content, $status, $draft, $keywords, $description);
		if ($ret) {
			header("Location: /" . APP_ADMIN_URI . "/post/list");
		}
	}

}